$(document).ready(function(){
  var ty_success_callback = function(response_data, http_code){
      
    if (typeof(response_data.msg) == 'object'){
      $(".content-wrapper").append(response_data.msg);
      window.location.reload()
      return;
    }
    if (response_data.code == 0){
      alert("success :" + response_data.msg)
    } else {
      alert("error :" + response_data.code+" msg:" + response_data.msg)
    }
  }
  var ty_error_callback = function (response_data){
    alert("save error" + response_data.msg)
  }


  // 跳转按钮
  $("body").on('click','.btn-goto',function($obj){
    $(location).attr('href', $(this).data('url'));  
  });

  $("body").on('click','.btn-goto-ajax',function($obj){

    $.ajax({
      type: 'GET',
      url: $(this).data('url'),
      data: {},
      success: ty_success_callback,
      error: ty_error_callback,
      dataType: 'JSON'
    });
  });

  // form submit 2 time
  $("form").submit(function(e){
    if ($(this).parent().attr("class").indexOf('modal-content') != -1)
    {
      return true;
    }
    var data = $(this).serializeArray();
    $.ajax({
      type: 'POST',
      url: $(this).attr("action"),
      data: data,
      success: ty_success_callback,
      error: ty_error_callback,
      dataType: 'JSON'
    });
    return false;
  });

    //$('body').on("confirm", ".confirm-ajax", function($obj){ 
        
        $('.confirm-ajax').confirm({
            text: "Are you sure you want to do this?",
            title: "Confirmation required",
            confirm: function(button) {
                //console.log("test confirm :"+ button.attr('data-url'));
                $.ajax({
                  type: 'GET',
                  url: button.data('url'),
                  data: {},
                  success: ty_success_callback,
                  error: ty_error_callback,
                  dataType: 'JSON'
                });
    
            },
            cancel: function(button) {
                // nothing to do
            },
            confirmButton: "Yes I am",
            cancelButton: "No",
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "modal-dialog modal-lg" // Bootstrap classes for large modal
        });
    //});

});

